#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "transmite.pb.h"
#include "utils.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试模式, true-发布模式");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志文件的路径");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志的输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务注册中心的地址");
DEFINE_string(base_service, "/service", "这是服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "这是服务监控根目录");

suye::ServiceManager::ptr sm;

void string_message(const std::string& uid, const std::string& sid, const std::string& msg)
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::MsgTransmitService_Stub stub(channel.get());
    suye::NewMessageReq req;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(suye::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);
    suye::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void image_message(const std::string& uid, const std::string& sid, const std::string& msg)
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::MsgTransmitService_Stub stub(channel.get());
    suye::NewMessageReq req;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(suye::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);
    suye::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string& uid, const std::string& sid, const std::string& msg)
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::MsgTransmitService_Stub stub(channel.get());
    suye::NewMessageReq req;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(suye::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);
    suye::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string& uid, const std::string& sid, 
        const std::string& filename, const std::string& content)
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    suye::MsgTransmitService_Stub stub(channel.get());
    suye::NewMessageReq req;
    req.set_request_id(suye::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(suye::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());
    suye::GetTransmitTargetRsp rsp;
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    suye::Init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 1.先构造Rpc信道管理对象
    sm = std::make_shared<suye::ServiceManager>();
    sm->Declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&suye::ServiceManager::OnServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&suye::ServiceManager::OnServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    // 2.构造服务发现对象
    suye::Discovery::ptr dclient = std::make_shared<suye::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // string_message("c5d8-6b47647d-0000", "会话ID1", "吃饭了吗？");
    // string_message("e863-027eba23-0001", "会话ID1", "吃的盖浇饭！");
    // image_message("c5d8-6b47647d-0000", "会话ID1", "这是一张图片");
    // speech_message("c5d8-6b47647d-0000", "会话ID1", "这是一段语音");
    file_message("c6e2-5a2e4533-0002", "37dc-bccd9269-0003", "猪妈妈的文件名称", "猪妈妈的文件数据");
    
    return 0;
}